<!--
 * @Description: 选择农场页面
-->
<template>
  <div class="choose-company" v-loading="loading">
    <Header />
    <!-- 选择系统 -->
    <div class="system-choose-modal" :class="{ active: showSystemChooseModal }">
      <div class="system-choose-box">
        <div class="system-choose-title">选择系统</div>
        <div class="system-content">
          <div class="system-list">
            <!-- 卡片式 -->
            <template v-for="(item, index) in systemList">
              <div :key="index" class="system-choose-item" @click="chooseSystem(item)">
                <div class="img">
                  <img v-if="item.src" :src="item.src" alt="">
                </div>
                <div class="name">{{ item.SystemName }}</div>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Header from "@/layout/components/Header/index.vue";
import UserApi from "@/api/system-settings/user-manage"
import Siderbar from "./components/Siderbar";
import HomeBaseApi from "@/api/basic-info-manage/homeBase";
import { setToken } from "@/utils/auth";
import axios from "axios";
export default {
  name: "ChooseSystem",
  components: { Header, Siderbar },
  data() {
    return {
      loading: false,
      account: '',
      userId: '',
      userType: '',
      showSystemChooseModal: false, // 选择系统弹窗
      functionPermissionMap: {},
      isNeedTss: false, //客户特殊需求：将溯源系统融入智慧渔业生产系统   防止sb客户变卦通过这个标记判断客户原需求和新需求
      systemList: [
        {
          src: '',
          SystemName: '智慧渔业管理系统',
          path: '/choose-company',
          name: 'chooseCompany',
          routerFlag: 'WEB'
        },
        {
          src: '',
          SystemName: '运营管理系统',
          path: '/oms-baseOvervie',
          name: 'baseOvervie',
          routerFlag: 'OMS'
        }, {
          src: '',
          SystemName: '智慧渔业驾驶舱',
          url: 'http://fishery.ssagroup.cn:8080/socialized-services/#/login',
          isMultilink: true,
          routerFlag: 'BIGSCREENCFI'
        }, {
          src: '',
          SystemName: '社会化服务',
          url: 'http://fishery.ssagroup.cn:8080/socialized-services/#/login',
          isMultilink: true,
          routerFlag: 'WEB'
        }]
    };
  },
  computed: {
  },
  created() {
    //选择系统时 刷新页面重置路由
    this.initPage();
    this.queryLoginUser()
    this.filterSystem();
  },
  methods: {
    //刷新页面
    initPage() {
      //路由 拼接“#reloaded”标记 判断路由跳转回页面时是否需要刷新页面
      if (window.location.href.indexOf("#reloaded") == -1) {
        window.location.href = window.location.href + "#reloaded";
        window.location.reload();
      } else {
        this.queryLoginUser()
      }
    },
    gotoBigscreencfi() {
      let url = 'http://fishery.ssagroup.cn:8080/bigscreencfi/#/login?token=' + sessionStorage.getItem('token') + '&homeBaseId=' + sessionStorage.getItem('homeBaseId')
      url = url
      window.open(url)
    },
    toSocializedServices() {
      axios({
        url: this.$baseurl.webServiceUrl + 'userManagement/user/createAccessToken/sociallize',
        method: "get",
        headers: {
          Token: sessionStorage.token,
          Homebaseid: sessionStorage.getItem('homeBaseId')
        }
      }).then(res => {
        if (res.data && res.data.success) {
          if (this.newWebPage) {
            this.newWebPage.close()
          }
          this.newWebPage = window.open('http://fishery.ssagroup.cn:8080/socialized-services/#/login?accessToken=' + res.data.data)
        }
      });
    },
    // 选择系统
    async chooseSystem(item) {
      if (item.isMultilink) {
        if (item.routerFlag == 'BIGSCREENCFI') {
          this.gotoBigscreencfi()
        } else {
          this.toSocializedServices()
        }
      } else {
        sessionStorage.setItem('routerFlag', item.routerFlag)
        this.authPermissionRoutes(item)
      }
    },
    async authPermissionRoutes(item) {
            //start======对接NC接口
      //     queryMeasdoc,//计量单位条件查询
      //     queryOrgDept,//部门条件查询
      //     queryOrgOrgs,//组织条件
      //     queryPsndoc,//人员基本信息
      //     queryResaCostcenter,//成本中心
      //     queryStordoc,//品级条件查询
      //     querySupplier,//供应商条件查询
      // 这里分一点nc基础数据接口请求，在choose-company.vue里也有一点
      // this.$store.dispatch("NcBasicInfo/queryMeasdoc");
      // this.$store.dispatch("NcBasicInfo/queryOrgDept");
      // this.$store.dispatch("NcBasicInfo/queryOrgOrgs");
      // this.$store.dispatch("NcBasicInfo/queryPsndoc");
      // this.$store.dispatch("NcBasicInfo/queryResaCostcenter");
      // this.$store.dispatch("NcBasicInfo/queryStordoc");
      // this.$store.dispatch("NcBasicInfo/querySupplier");

      //  end======对接NC接口
      this.$router.push({ name: item.name });
    },
    queryLoginUser() {
      this.loading = true;
      UserApi.getLoginUser().then(res => {
        this.loading = false;
        this.userId = res.data.userId  //集团层 userId=1
        this.account = res.data.account  //集团层 admin
        this.userType = res.data.userType//res.data.userType  1:运营  2:基地
        this.functionPermissionMap = res.data.functionPermissionMap
        setToken('staffId', res.data.staffId || '')
        setToken('userId', res.data.userId || '')
        setToken('userType', res.data.userType || '')
        setToken('account', res.data.account || '')
        this.getHomeBase();
        this.filterSystem();
      }).catch(err => {
        this.loading = false;
      })
    },
    filterSystem(){
      let userType = sessionStorage.getItem('userType')
      if (userType == '2') {
          this.systemList = this.systemList.filter(item => {
            return item.routerFlag == 'BIGSCREENCFI' || item.routerFlag == 'WEB'
          })
        }
    },
    // 获取当前用户可查看基地列表
    getHomeBase() {
      // 获取顶级组织结构id
      this.loading = true;
      HomeBaseApi.queryByUserId().then(res => {
        this.loading = false;
        if (res.length == 0 || res.data == []) {
          this.$message.warning("该账户下无基地权限，请联系管理员！")
          removeToken("token")
          this.$router.push({ path: "/login" })
        } else {
          //默认第一个基地
          setToken("companyMark", res.data[0].code);
          setToken("homeBaseId", res.data[0].homeBaseId);
          setToken("homeBaseType", res.data[0].homeBaseType);
          setToken("homeBaseName", res.data[0].homeBaseName);
          setToken("homeBaseList", JSON.stringify(res.data));
        }
      }).catch(err=>{
        this.loading = false;
      })
    },
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          homeBaseName: this.homeBaseName
        }
      };
      homeBaseApi.queryPageByCondition(params).then(res => {
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },
  }
};
</script>

<style scoped lang="scss">
@import "~@/styles/variables.scss";
$slidebarWidth: 40px;
$highBule: rgb(0, 235, 208);

@keyframes borderbg {
  from {
    border-radius: 0;
  }

  to {
    background-color: #fff;
    color: #333;
    border-radius: 20px;
  }
}

.choose-company {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;

  ::v-deep(.header) {
    flex-shrink: 0;
  }


}

.system-choose-modal {
  position: relative;
  width: calc(100% - 20px);
  height: calc(100% - 68px);
  margin: 0 10px;
  background-image: url("~@/assets/choose-company/back.jpg");
  background-repeat: no-repeat;
  background-position: top center;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 40px;
  opacity: 1;
  transition: all .5s;

  // pointer-events: none;
  &.active {
    opacity: 1;
    pointer-events: auto;
  }

  .add-basehome-btn {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 112px;
    height: 26px;
    background: #0480FB;
    box-shadow: 0px 0px 2px 1px rgba(0, 0, 0, 0.26);
    border-radius: 3px;
    color: #fff;
    text-align: center;
    line-height: 26px;
    cursor: pointer;

    .el-icon-plus {
      border: 1px dashed #fff;
      color: #fff;
    }
  }

  .system-choose-box {
    width: 100%;
    height: 100%;
    padding: 12px 0;

    .system-choose-title {
      position: absolute;
      top: 25%;
      left: 50%;
      width: 137px;
      height: 35px;
      color: #fff;
      transform: translate(-50%, -50%);
      background: rgba(0, 0, 0, 0.25);
      border-radius: 7px;
      border: 1px solid #FFFFFF;
      text-align: center;
      line-height: 32px;
      letter-spacing: 2px;
    }

    .system-content {
      height: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
      position: relative;

      .system-list {
        width: 100%;
        height: 90%;
        display: flex;
        justify-content: space-around;
        align-items: center;
        overflow-x: auto;

        .system-choose-item {
          position: relative;
          color: #fff;
          width: 330px;
          height: 240px;
          border: 1px solid #7CD2FF;
          transition: all 0.5s;
          cursor: pointer;

          .img {
            height: calc(100% - 46px);
            // width: 100%
            background: url('~@/assets/choose-company/ly.jpg') center center no-repeat;
            background-size: 100% 100%;
            display: flex;
            flex-direction: row;
            // flex-wrap: wrap;
            justify-content: center;
            align-content: center;
            align-items: center;

            img {
              width: 100%;
              height: 100%;
            }
          }

          .name {
            text-align: center;
            font-size: 16px;
            height: 46px;
            line-height: 46px;
            background-color: #368DFF;
          }

          &:hover {
            transform: scale(1.04);
          }
        }
      }
    }
  }

}
</style>

<style lang="scss">
.el-scrollbar__wrap {
  overflow-x: hidden;
}

.is-horizontal {
  display: none;
}
</style>
